package pl.vanta.utils.gui.dialogs;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.KeyStroke;
import javax.swing.SwingConstants;

import pl.vanta.utils.commons.managers.ResourceBundleManager;
import pl.vanta.utils.gui.actions.CloseWindowAction;

public class OkCancelDialog<E> extends JDialog implements KeyListener, ActionListener
{
	private static final long				serialVersionUID		= 1L;

	private static final String				ID_BTN_OK				= "ok";
	private static final String				ID_BTN_CANCEL			= "cancel";

	private JButton							btnOk;
	private JButton							btnCancel;

	private AbstractDialogContainer<E>		panel;

	private static ResourceBundleManager	resourceBundleManager	= new ResourceBundleManager(OkCancelDialog.class, null);

	private boolean							okClicked				= false;

	public OkCancelDialog(JFrame parent, AbstractDialogContainer<E> panel)
	{
		super(parent);

		this.panel = panel;

		init();
	}

	public OkCancelDialog(JDialog parent, AbstractDialogContainer<E> panel)
	{
		super(parent);

		this.panel = panel;

		init();
	}

	public OkCancelDialog(Window parent, AbstractDialogContainer<E> panel)
	{
		super(parent);

		this.panel = panel;

		init();
	}

	private void init()
	{
		this.btnOk = new JButton(resourceBundleManager.get(ID_BTN_OK));
		this.btnCancel = new JButton(resourceBundleManager.get(ID_BTN_CANCEL));

		this.btnOk.setActionCommand(ID_BTN_OK);
		this.btnCancel.setActionCommand(ID_BTN_CANCEL);

		this.btnOk.addActionListener(this);
		this.btnCancel.addActionListener(this);

		KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
		this.getRootPane().getActionMap().put("esc", new CloseWindowAction(this));
		this.getRootPane().getInputMap().put(stroke, "esc");

		this.setModal(true);
		this.setTitle(this.panel.getTitle());
		this.setResizable(false);

		this.getContentPane().setLayout(new BorderLayout());
		this.getContentPane().add(this.panel, BorderLayout.CENTER);
		this.getContentPane().add(createButtonsPanel(), BorderLayout.SOUTH);

		this.pack();
		this.setLocationRelativeTo(super.getOwner());
	}

	private JPanel createButtonsPanel()
	{
		JPanel buttonsPanel = new JPanel(new FlowLayout(SwingConstants.RIGHT));

		this.btnOk = new JButton(resourceBundleManager.get(ID_BTN_OK));
		this.btnCancel = new JButton(resourceBundleManager.get(ID_BTN_CANCEL));

		this.btnOk.setActionCommand(ID_BTN_OK);
		this.btnCancel.setActionCommand(ID_BTN_CANCEL);

		this.btnOk.addActionListener(this);
		this.btnCancel.addActionListener(this);

		buttonsPanel.add(this.btnOk);
		buttonsPanel.add(this.btnCancel);

		JPanel allPanel = new JPanel(new BorderLayout());

		allPanel.add(new JSeparator(), BorderLayout.NORTH);
		allPanel.add(buttonsPanel, BorderLayout.CENTER);

		return allPanel;
	}

	public E getDialogBean()
	{
		return this.panel.getDialogBean();
	}

	public E showDialog(E bean)
	{
		if (bean != null)
		{
			this.panel.setDialogBean(bean);
		}

		this.setVisible(true);

		if (this.okClicked)
		{
			return this.getDialogBean();
		}
		else
		{
			return null;
		}
	}

	@Override
	public void actionPerformed(ActionEvent e)
	{
		if (ID_BTN_OK.equals(e.getActionCommand()))
		{
			ok();
		}
		else if (ID_BTN_CANCEL.equals(e.getActionCommand()))
		{
			cancel();
		}
	}

	private void cancel()
	{
		this.setVisible(false);
	}

	private void ok()
	{
		if (this.panel.canClose())
		{
			this.okClicked = true;

			this.setVisible(false);
		}
	}

	@Override
	public void keyPressed(KeyEvent e)
	{
		System.out.println("keyPressed");
	}

	@Override
	public void keyReleased(KeyEvent e)
	{
		System.out.println("keyReleased");
	}

	@Override
	public void keyTyped(KeyEvent e)
	{
		System.out.println(e.getKeyCode());
	}
}